package com.vast.metrix.viewer.bustraffic.controllers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;

import com.vast.ui.component.state.Sequence;
import com.vast.ui.component.toolbar.ToolBarAction;
import com.vast.ui.component.util.ConsoleLoggerSingleton;
import com.vast.ui.component.util.ImportDialog;
import com.vast.ui.component.util.Logger;

import com.vast.metrix.viewer.bustraffic.models.BusTraffic;
import com.vast.metrix.viewer.bustraffic.models.BusTrafficCollection;
import com.vast.metrix.viewer.bustraffic.viewers.BusTrafficViewer;
import com.vast.metrix.viewer.bustraffic.views.ViewPart;

public class InterfaceController  extends ToolBarAction implements IRefreshListener {
	private final Display display;

	private String[] busNames = null;
	private int      samplingRate_ms = 10;
	private int      xAxisLength_ms  = 500;
	private boolean isReadyForReset;
	private final BusTrafficViewer viewer;
	private AutoRefreshFire         fireController = null;
	private int                     xAxisLengthMilliseconds	= 1000;
	private ImportDialog importDialog;
	public  final Logger     logger;
	
	public InterfaceController(ViewPart view, Composite parent, int serverPort) {
		super(view);
		this.display                 = parent.getDisplay();
		this.logger                  = ConsoleLoggerSingleton.getInstance();
		this.viewer                  = new BusTrafficViewer(parent, "BusTraffic Viewer");
		
		this.isReadyForReset = false;
		
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot root  = workspace.getRoot();
		String initialSavePath = root.getLocation().toOSString();
		String[] filterNames = new String [] {"CSV Files", "All Files (*)"};
		String[] filterExtensions = new String [] {"*.csv;*.txt;*.", "*"};
		this.importDialog = new ImportDialog(parent, initialSavePath, filterNames, filterExtensions);
		
	}
	
	public void refresh() {
		// TODO Auto-generated method stub
		this.display.asyncExec(new Runnable() {
			public void run() {
				/*
				for (int i = 0; i < busTrafficCollectionArray.length; i++) {
					BusTraffic maximumValue = busTrafficCollectionArray[i].getMaximumValue();
					viewer.updateUpperValueOfRangeAxis(i, maximumValue, false);
				}
				*/
			}
		});
		
	}

	public void importCsvFile() {
		String fileName = this.importDialog.openImportDialog("*", false);
		if(fileName != null) {
				
			try {
		        String line;
		
		        BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
		        
		        while( (line = bufferedReader.readLine()) != null ) {
		        	if(line.startsWith("#")) {
		        		;
		        	}  else if (line.startsWith("info")) {
		        		String[] infoHeader = new String[2];
		        		String temp[] = line.split(":");
		        		infoHeader[0] = temp[0].substring(temp[0].indexOf("(") + 1, temp[0].length() - 1);
		        		infoHeader[1] = temp[1];
		        
		        		if(infoHeader[0].startsWith("SamplingRate_ms=")) {
		        			String[] data = infoHeader[0].split("=");
		        			this.samplingRate_ms = Integer.parseInt(data[1]);
		        		}	        	
		        		if (infoHeader[1].startsWith("BusName=")) {
		        			String[] data =infoHeader[1].split("=");
		        			this.busNames = data[1].split(";");
		        			this.viewer.createChart(this.busNames, this.samplingRate_ms);
		        		}
		        	} else {
		        		String[] data = line.split(",");
		        		int j = 0;
		        		Long currentTime_ms = Long.parseLong(data[j]);
		        		j++;
		        		for(int i = 0; i < this.busNames.length; i++){
		        			double transferByte = Double.parseDouble(data[j]);
		        			double activity = Double.parseDouble(data[j + 1]);
		        			double latency = Double.parseDouble(data[j + 2]);
		        			double utilization = Double.parseDouble(data[j + 3]);
		        			j += 4;
		        			this.viewer.addBusTraffic(i, currentTime_ms, transferByte, activity, latency, utilization);
		        		}
		        	}
		        }
		        bufferedReader.close();
		       
		        viewer.fireSeriesChanged();
		        viewer.restoreAutoBounds();
		        
			} catch( IOException e ) {

			}
		}
	}
	
	@Override
	public void openFileAction() {
		importCsvFile();
	}

}
